package cn.hiapi.publics.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicController;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.system.ILocationService;
import cn.hiapi.interfaces.system.data.LocationData;
import cn.hiapi.publics.query.SearchQuery;
import cn.hiapi.publics.vo.ActivityVo;
import cn.hiapi.publics.vo.ShopBaseVo;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.entity.shop.ShopScore;
import cn.hiapi.shop.entity.shop.ShopType;
import cn.hiapi.shop.service.shop.ShopScoreService;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shop.service.shop.ShopTypeService;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.activity.ActivityCategory;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.activity.ActivityCategoryService;
import cn.hiapi.shopkeeper.service.activity.ActivityFactory;
import cn.hiapi.shopkeeper.service.activity.ActivityLotteryItemService;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/30 23:22
 */
@RestController("shopkeeperSearchController")
@RequestMapping("/public/search")
public class SearchController extends BasicController {

    private final DispatchContext dispatchContext;

    public SearchController(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }

    /**
     * 搜索服务
     *
     * @param query
     * @return
     */
    @PostMapping()
    public ResponseEntity<List> search(@RequestBody SearchQuery query) {
        query.setDirection(Sort.Direction.DESC);
        query.setProperties("id");
        if (StringUtils.equalsIgnoreCase(query.getType(), "shop")) {
            QueryWrapper wrapper = new QueryWrapper()
                    .eq("status", 1);
            if (!StringUtils.isEmpty(query.getKey())) {
                wrapper.like("name", StringUtils.join("%", query.getKey(), "%"));
            }

            ResponseEntity<List<Shop>> response = this.dispatchContext.getServiceOne(ShopService.class).findPage(wrapper, query.getPageable());
            List<JSONObject> collect = response.getData().stream().map(e -> {
                StringBuffer address = new StringBuffer();
                LocationData locationData = this.dispatchContext.getServiceOne(ILocationService.class).locationGet(e.getStateId());
                if (locationData != null)
                    address.append(locationData.getName()).append(" ");
                locationData = this.dispatchContext.getServiceOne(ILocationService.class).locationGet(e.getCityId());
                if (locationData != null)
                    address.append(locationData.getName()).append(" ");
                locationData = this.dispatchContext.getServiceOne(ILocationService.class).locationGet(e.getDistrictId());
                if (locationData != null)
                    address.append(locationData.getName()).append(" ");
                address.append(e.getAddress());
                ShopType type = this.dispatchContext.getServiceOne(ShopTypeService.class).get(e.getCid());

                ShopScore score = this.dispatchContext.getServiceOne(ShopScoreService.class).getByShopId(0, e.getId());
                return JsonUtils.builder()
                        .put("id", e.getId())
                        .put("name", e.getName())
                        .put("logo", e.getLogo())
                        .put("lat", e.getLat())
                        .put("lng", e.getLng())
                        .put("score", score != null ? score.getComplexScore() : 5)
                        .put("address", address.toString())
                        .put("cid", e.getCid())
                        .put("type", type != null ? type.getName() : "")
                        .build();


            }).collect(Collectors.toList());
            return toSuccess(collect, response.getTotal(), response.getTotalPage());
        } else {
            QueryWrapper wrapper = new QueryWrapper();
            if (StringUtils.isNotEmpty(query.getKey())) {
                wrapper.like("title", StringUtils.join("%", query.getKey(), "%"));
            }
            wrapper.eq("type", query.getActivityType());
            ResponseEntity<List<Activity>> response = this.dispatchContext.getServiceOne(ActivityService.class).findPage(wrapper, query.getPageable());
            List<ActivityVo> collect = response.getData().stream().map(e -> ActivityController.getActivityVo(e, this.dispatchContext)).collect(Collectors.toList());
            return toSuccess(collect, response.getTotal(), response.getTotalPage());
        }


    }


}
